package com.qf.web.servlet;

import com.qf.pojo.Goods;
import com.qf.pojo.PageBean;
import com.qf.service.GoodsService;
import com.qf.service.impl.GoodsServiceImpl;
import com.qf.utils.StrUtils;

import javax.servlet.annotation.WebServlet;

/**
 * @author 千锋教育
 * @Company http://www.mobiletrain.org/
 * @Version 1.0
 */
@WebServlet("/goodsservlet")
public class GoodsServlet extends BaseServlet {

    GoodsService goodsService = new GoodsServiceImpl();

    public String getGoodsById(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws Exception {
        int id = Integer.parseInt(request.getParameter("id"));
        Goods goods = goodsService.findById(id);

        request.setAttribute("goods", goods);
        return "forward:/goodsDetail.jsp";
    }


    public String getGoodsListByTypeId(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws Exception {
        //获取分类id
        int typeId = Integer.parseInt(request.getParameter("typeId"));
        String pageNumStr = request.getParameter("pageNum");
        String pageSizeStr = request.getParameter("pageSize");
        //默认值
        int pageNum = 1;
        int pageSize = 2;
        if(StrUtils.isNotEmpty(pageNumStr)) {
            pageNum = Integer.parseInt(pageNumStr);
        }

        if(StrUtils.isNotEmpty(pageSizeStr)) {
            pageSize = Integer.parseInt(pageSizeStr);
        }


        PageBean pageBean = goodsService.findByPage(typeId, pageNum, pageSize);

        request.setAttribute("pageBean", pageBean);
        request.setAttribute("typeId", typeId);

        return "forward:/goodsList.jsp";

    }

}
